package level.editor.control.tile;


import utility.ImageUtility;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import level.editor.control.layer.Layer;
import level.editor.control.layer.LayerBuffer;
import level.editor.control.layer.LayerImage;
import level.editor.control.main.Main;
import level.model.Tile;

public class TileBuffer extends LayerBuffer
{
	public TileBuffer(Integer zOrder, Point point, Tile tile, Main main)
	{
		super(zOrder, main);
		initialPoint = point;
		endPoint = point;
		this.tile = tile;
		try
		{
			image = ImageUtility.makeColorTransparent(ImageIO.read(new File(
					tile.getIconPath())), new Color(255, 0, 255));
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public void setEndPoint(Point point)
	{
		endPoint = point;
	}

	public void finishRectangle(Layer layer)
	{
		int width = (int) Math.abs(initialPoint.getX() - endPoint.getX())
				+ TileConstants.TILE_SIZE;
		int height = (int) Math.abs(initialPoint.getY() - endPoint.getY())
				+ TileConstants.TILE_SIZE;
		((TileLayer) layer).addRectangle(tile, calculateTopLeft(), height
				/ TileConstants.TILE_SIZE, width / TileConstants.TILE_SIZE,
				getImage());
		main.setDirty(Main.DIRTY);
		main.createMemento();
		main.remove(this);
	}

	public LayerImage getImage()
	{
		setSize(getParent().getSize());
		Point topLeft = calculateTopLeft();
		int width = (int) Math.abs(initialPoint.getX() - endPoint.getX())
				+ TileConstants.TILE_SIZE;
		int height = (int) Math.abs(initialPoint.getY() - endPoint.getY())
				+ TileConstants.TILE_SIZE;
		int originX = (int) main.getOrigin().getX();
		int originY = (int) main.getOrigin().getY();

		LayerImage newImage = new LayerImage(width, height);
		BufferedImage oldImage = new BufferedImage((int) getSize().getWidth(),
				(int) getSize().getHeight(), BufferedImage.TYPE_INT_ARGB);

		Graphics2D g = oldImage.createGraphics();
		paint(g);
		g.dispose();

		g = newImage.createGraphics();
		g.drawImage(oldImage, 0, 0, width, height, (int) topLeft.getX()
				+ originX, (int) topLeft.getY() + originY, (int) topLeft.getX()
				+ originX + width, (int) topLeft.getY() + originY + height,
				null);

		newImage.setTop((int) topLeft.getY());
		newImage.setLeft((int) topLeft.getX());

		return newImage;
	}

	@Override
	public void paintComponent(Graphics g)
	{
		setSize(getParent().getSize());

		Point topLeft = calculateTopLeft();
		int width = (int) Math.abs(initialPoint.getX() - endPoint.getX())
				+ TileConstants.TILE_SIZE;
		int height = (int) Math.abs(initialPoint.getY() - endPoint.getY())
				+ TileConstants.TILE_SIZE;

		int originX = (int) main.getOrigin().getX();
		int originY = (int) main.getOrigin().getY();

		for (int x = (int) topLeft.getX(); x < topLeft.getX() + width; x += TileConstants.TILE_SIZE)
		{
			for (int y = (int) topLeft.getY(); y < topLeft.getY() + height; y += TileConstants.TILE_SIZE)
			{
				// Placeholder rendering

				// g.setColor(Color.RED);
				// g.drawRect(x + originX, y + originY, TileConstants.TILE_SIZE,
				// TileConstants.TILE_SIZE);

				g.drawImage(image, x + originX, y + originY, x + originX
						+ TileConstants.TILE_SIZE, y + originY
						+ TileConstants.TILE_SIZE, tile.getSx(), tile.getSy(),
						tile.getSx() + tile.getSw(),
						tile.getSy() + tile.getSh(), null);
			}
		}
	}

	private Point calculateTopLeft()
	{
		Point point = new Point();
		point.x = initialPoint.getX() < endPoint.getX() ? (int) initialPoint
				.getX() : (int) endPoint.getX();
		point.y = initialPoint.getY() < endPoint.getY() ? (int) initialPoint
				.getY() : (int) endPoint.getY();
		return point;
	}

	private Point initialPoint;
	private Point endPoint;
	private Tile tile;
	private Image image;
}
